package com.dubbo.order.service.impl;



import com.dubbo.common.dtos.PageResponseResult;
import com.dubbo.common.dtos.ResponseResult;
import com.dubbo.model.balance.dtos.PageRequestDto;
import com.dubbo.model.order.dtos.MountingsDTO;
import com.dubbo.model.order.dtos.MountingsOrderDTO;


import com.dubbo.model.order.pojos.MountingsOrder;
import com.dubbo.model.order.pojos.MountingsOrderInfo;
import com.dubbo.model.order.pojos.MountingsOrderOperate;


import com.dubbo.order.MountingsOrderService;
import com.alibaba.dubbo.config.annotation.Service;
import com.dubbo.order.mapper.MountingsOrderInfoMapper;
import com.dubbo.order.mapper.MountingsOrderMapper;
import com.dubbo.order.mapper.MountingsOrderOperateMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Service(timeout = 6000000, retries = 0)
@Component
public class MountingsOrderServiceImpl implements MountingsOrderService {
    @Autowired
    private MountingsOrderMapper mountingsOrderMapper;
    @Autowired
    private MountingsOrderInfoMapper orderInfoMapper;
    @Autowired
    private MountingsOrderOperateMapper operateMapper;

    //列表展示
    @Override
    public ResponseResult<List<MountingsOrder>> paretsOrderList(PageRequestDto dto) {
        //设置页码大小
        dto.checkParam();
        Integer dtoPage = dto.getPage();
        dto.setPage((dtoPage - 1) * dto.getSize());

        Integer count = mountingsOrderMapper.count();
        List<MountingsOrder> orderList = mountingsOrderMapper.paretsOrderList(dto);

        PageResponseResult pageResponseResult = new PageResponseResult(dtoPage,dto.getSize(),count);
        pageResponseResult.setData(orderList);

        return pageResponseResult;
    }

    @Override
    public ResponseResult<MountingsDTO> findById(Long id) {

        MountingsOrder mountingsOrder = mountingsOrderMapper.findById(id);
        List<MountingsOrderInfo> orderInfoList = orderInfoMapper.findByOrderId(mountingsOrder.getId());
        List<MountingsOrderOperate> operateList = operateMapper.findByOrderId(mountingsOrder.getId());

        MountingsDTO mountingsDTO = new MountingsDTO();
        if (mountingsOrder != null){
            mountingsDTO.setMountingsOrder(mountingsOrder);
        }

        if (orderInfoList != null){
            mountingsDTO.setMountingsOrderInfoList(orderInfoList);
            double totalMoney=0;
            for (MountingsOrderInfo orderInfo : orderInfoList) {
                totalMoney+=orderInfo.getTotalMoney();
            }
            mountingsDTO.setTotalMoney(totalMoney);
        }

        if (operateList !=null){
            mountingsDTO.setMountingsOrderOperateList(operateList);
        }


        return ResponseResult.okResult(mountingsDTO);
    }

    //条件查询
    @Override
    public ResponseResult<List<MountingsOrder>> selectByPage(MountingsOrderDTO dto) {

        //设置页码大小
        dto.checkParam();
        Integer dtoPage = dto.getPage();
        dto.setPage((dtoPage - 1) * dto.getSize());

        List<MountingsOrder> mountingsOrders = mountingsOrderMapper.selectByPage(dto);
        Integer count = mountingsOrderMapper.selectCount(dto);

        PageResponseResult pageResponseResult = new PageResponseResult(dtoPage,dto.getSize(),count);
        pageResponseResult.setData(mountingsOrders);

        return pageResponseResult;
    }
}
